package com.weayer.remote;

import com.alibaba.fastjson.JSONObject;
import com.weayer.constant.AmapConfigConstants;
import com.weayer.dto.ResponseResult;
import com.weayer.request.PointDTO;
import com.weayer.request.PointRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @description: 轨迹点管理Client
 * @author: Weayer
 **/
@Service
public class MapPointsClient {
    @Value("${amap.key}")
    private String amapKey;
    @Value("${amap.sid}")
    private String amapSid;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * @Description: 轨迹点上传
     * @Param: [pointRequest]
     * @return: com.weayer.dto.ResponseResult
     **/
    public ResponseResult upload(PointRequest pointRequest){
        // 1. 封装post表单请求
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(){
            {
                add("key", amapKey);
                add("sid", amapSid);
                add("tid", pointRequest.getTid());
                add("trid", pointRequest.getTrid());
                add("points", JSONObject.toJSONString(pointRequest.getPoints()));
            }
        };
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        // 2. 调用高德地图api
        String jsonString = restTemplate.postForObject(AmapConfigConstants.TRACK_POINT_UPLOAD_URL_PREFIX, request, String.class);
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        // 1. 组装url:
//        StringBuilder urlBuilder = new StringBuilder();
//        urlBuilder.append(AmapConfigConstants.TRACK_POINT_UPLOAD_URL_PREFIX);
//        urlBuilder.append("?");
//        urlBuilder.append("key=" + amapKey);
//        urlBuilder.append("&");
//        urlBuilder.append("sid=" + amapSid);
//        urlBuilder.append("&");
//        urlBuilder.append("tid=" + pointRequest.getTid());
//        urlBuilder.append("&");
//        urlBuilder.append("trid=" + pointRequest.getTrid());
//        urlBuilder.append("&");
//        urlBuilder.append("points=[");
//        PointDTO[] points = pointRequest.getPoints();
//        for(PointDTO point: points){
//            String location = point.getLocation();
//            String locatetime = point.getLocatetime();
//            urlBuilder.append("{\"location\":{"+ location + "},");
//            urlBuilder.append("{\"locatetime\":{" + locatetime + "}");
//        }
//        urlBuilder.append("]");
        // 2. 调用高德地图api
//        ResponseEntity<String> serviceEntity = restTemplate.postForEntity(urlBuilder.toString(), null, String.class);
        return ResponseResult.success("");
    }
}
